/*
 * Licensed to the anubis
 *
 * Copyright (c) 2023 .
 * All rights reserved.
 * 项目名称：anubis-demo-web
 * 项目描述：基于spring5的spring-mvc新特性
 * 版权说明：本软件属XXX有限公司所有，在未获得XXX有限公司正式授权情况下，任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 */
package com.anubis.demo.service.impl;

import com.anubis.demo.dao.StockSubscriptionDAO;
import com.anubis.demo.dao.dataobject.StockSubscriptionDO;
import com.anubis.demo.service.PriceQueryEngine;
import com.anubis.demo.service.StockPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class StockPriceServiceImpl implements StockPriceService {

    @Autowired
    private StockSubscriptionDAO subscriptionDao;

    @Autowired
    private PriceQueryEngine priceQueryEngine;

    @Override
    public Map<String, String> getPrice(String email) {
        List<StockSubscriptionDO> subscriptions = subscriptionDao.findByEmail(email);
        Map<String, String> priceMap = subscriptions.stream()
                .map(subscription -> subscription.getSymbol())
                .collect(Collectors.toMap(
                                Function.identity(),
                                priceQueryEngine::getPriceForSymbol
                        )
                );
        return priceMap;
    }

}
